package me.like.diandian.fragment;


import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import me.like.diandian.R;
import me.like.diandian.adapter.HomeAdapter;
import me.like.diandian.api.Api;
import me.like.diandian.base.BaseConstant;
import me.like.diandian.base.BaseFragment;
import me.like.diandian.databinding.FragmentHomeBinding;
import me.like.diandian.requestbean.BannerRequest;
import me.like.diandian.util.RxUtils;

/**
 * Created by shousakai on 2019/6/21.
 */

public class HomeFragment extends BaseFragment<FragmentHomeBinding> {
    private HomeAdapter adapter;

    @Override
    protected void initView() {
        LinearLayoutManager manager = new LinearLayoutManager(activity);
        binding.llRefresh.idRecyclerview.setLayoutManager(manager);
        adapter = new HomeAdapter(activity, manager);
        binding.llRefresh.llParent.setBackgroundColor(ActivityCompat.getColor(activity, R.color.common_bg));
        binding.llRefresh.idRecyclerview.setAdapter(adapter);
        binding.tlvTitle.setTitle(getString(R.string.tab1_title));
        binding.tlvTitle.setBackgroundColor(ActivityCompat.getColor(activity, R.color.white));
        refresh();
        binding.llRefresh.refresh.setEnableLoadmore(false);
        binding.llRefresh.refresh.setOnRefreshListener(refreshlayout -> {
            refresh();
        });
    }

    private void refresh() {
        Api.getInstance().getService().
                getLoanMarketList("0", BaseConstant.APPCHANNELID, "0", "0", "0")
                .compose(RxUtils.applySchedulerAndAllFilter(activity))
                .subscribe(data -> {
                    adapter.setListBeans(data.getData());
                    binding.llRefresh.refresh.finishRefresh();
                }, Throwable::printStackTrace);
        Api.getInstance().getService().getBannerList(new BannerRequest()).compose(RxUtils.applySchedulerAndAllFilter(activity))
                .subscribe(data -> {
                    if (data != null)
                        adapter.setBannerListBeans(data.getData());
                    binding.llRefresh.refresh.finishRefresh();
                }, Throwable::printStackTrace);
        Api.getInstance().getService().bannerAnnounce("0", "100", "1").compose(RxUtils.applySchedulerAndAllFilter(activity))
                .subscribe(data -> {
                    if (data != null && data.getData() != null)
                        adapter.setBannerAnnounceList(data.getData().getList());
                    binding.llRefresh.refresh.finishRefresh();
                }, Throwable::printStackTrace);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }
}
